//
//  UIViewController+HWBackClickIntercept.m
//  OCDome
//
//  Created by 李含文 on 2020/10/10.
//  Copyright © 2020 李含文. All rights reserved.
//

#import "UIViewController+HWBackClickIntercept.h"
#import <objc/runtime.h>

//static NSString *const kOriginDelegateKEY = @"kOriginDelegateKEY";
//
//@implementation UIViewController (HWBackClickIntercept)
//
//// MARK: - 快速添加返回按钮 并让边缘手势生效
//- (void)hw_customBackButton {
//    if (!self.navigationController) {return;} // 如果没有导航控制器 什么都做 防呆
//    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(hw_backButtonClick)];
//    if (@available(iOS 11.0, *)) {
//        leftBarButtonItem.imageInsets = UIEdgeInsetsMake(2, -8, 0, 0);
//        self.navigationItem.leftBarButtonItem = leftBarButtonItem;
//    } else {
//        UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFixedSpace) target:nil action:nil];
//        nagetiveSpacer.width = -8; //这个值可以根据自己需要自己调整
//        self.navigationItem.leftBarButtonItems = @[leftBarButtonItem, leftBarButtonItem];
//    }
//    if (self.navigationController.viewControllers.count > 1 && self.navigationController.interactivePopGestureRecognizer.enabled == YES) { // 自定义返回按钮 回复边缘返回手势
//        self.navigationController.interactivePopGestureRecognizer.delegate = self.navigationController;
//    }
//}
//
//- (void)hw_backButtonClick {
//    if ([self respondsToSelector:@selector(hw_navigationShouldPopOnBackButton)] && ![self hw_navigationShouldPopOnBackButton]) {
//        return;
//    }
//    if (self.navigationController.childViewControllers.count == 1) {
//        [self dismissViewControllerAnimated:YES completion:nil];
//    } else {
//        [self.navigationController popViewControllerAnimated:YES];
//    }
//}
//
//@end

//@implementation UINavigationController (HWBackClickIntercept)
//
//+ (void)load {
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        Class class = [self class];
//        SEL originSelector = @selector(viewDidLoad);
//        SEL swizzledSelector = @selector(hw_viewDidLoad);
//        Method originMethod = class_getInstanceMethod(class, originSelector);
//        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
//        BOOL didAddMethod = class_addMethod(class,
//                                            originSelector,
//                                            method_getImplementation(swizzledMethod),
//                                            method_getTypeEncoding(swizzledMethod));
//        if (didAddMethod) {
//            class_replaceMethod(class,
//                                swizzledSelector,
//                                method_getImplementation(originMethod),
//                                method_getTypeEncoding(originMethod));
//        } else {
//            method_exchangeImplementations(originMethod, swizzledMethod);
//        }
//    });
//}
//- (void)hw_viewDidLoad {
//    [self hw_viewDidLoad];
//    objc_setAssociatedObject(self, [kOriginDelegateKEY UTF8String], self.interactivePopGestureRecognizer.delegate, OBJC_ASSOCIATION_ASSIGN);
//    self.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
//}
////MARK: 按钮控制
//- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
//    NSLog(@"按钮来了");
//    if ([self.viewControllers count] < [navigationBar.items count]) {
//        return YES;
//    }
//    BOOL shouldPop = YES;
//    UIViewController* vc = [self topViewController];
//    if([vc respondsToSelector:@selector(hw_navigationShouldPopOnBackButton)]) {
//        shouldPop = [vc hw_navigationShouldPopOnBackButton];
//    }
//    if(shouldPop) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self popViewControllerAnimated:YES];
//        });
//    } else {
//        for(UIView *subview in [navigationBar subviews]) {
//            if(0. < subview.alpha && subview.alpha < 1.) {
//                [UIView animateWithDuration:.25 animations:^{
//                    subview.alpha = 1.;
//                }];
//            }
//        }
//    }
//    return NO;
//}
//
//#pragma mark - UIGestureRecognizerDelegate
////MARK: 手势控制
//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
//    NSLog(@"手势来了");
//    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
//        UIViewController *vc = [self topViewController];
//        if([vc respondsToSelector:@selector(hw_navigationShouldPopOnBackButton)]) {
//            return [vc hw_navigationShouldPopOnBackButton];
//        }
//        id<UIGestureRecognizerDelegate> originDelegate = objc_getAssociatedObject(self, [kOriginDelegateKEY UTF8String]);
//        return [originDelegate gestureRecognizerShouldBegin:gestureRecognizer];
//    }
//    return YES;
//}
//@end
